package com.aisile.manager.controller;

import com.aisile.entity.PageResult;
import com.aisile.entity.Result;
import com.aisile.manager.BrandService;
import com.aisile.manager.util.Poi;
import com.aisile.pojo.TbBrand;
import com.alibaba.dubbo.config.annotation.Reference;
import excel.POIConstant;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Reference
    BrandService brandService;

    @RequestMapping("/list")
    public List<TbBrand> getList(){

        return brandService.findAll();
    }

    @RequestMapping("/search")
    public PageResult searchList(Integer page, Integer rows, @RequestBody TbBrand brand){

        PageResult pageResult=brandService.serachList(page,rows,brand);

        return pageResult;
    }
    @RequestMapping("/save")
    public Result save(@RequestBody TbBrand tbBrand){
        int i=brandService.save(tbBrand);
        if(i>0){
            return new Result(true,"添加成功");
        }else{
            return new Result(false,"添加失败");
        }

    }
    @RequestMapping("/findOne")
    public TbBrand findOne(Long id){

        return brandService.findOne(id);
    }
    @RequestMapping("/update")
    public Result update(@RequestBody TbBrand tbBrand){
        int i=brandService.update(tbBrand);
        if(i>0){
            return new Result(true,"修改成功");
        }else{
            return new Result(false,"修改失败");
        }
    }
    @RequestMapping("/delAll")
    public Result delAll(Long[] ids){
        int i=brandService.delAll(ids);
        if(i>0){
            return new Result(true,"删除成功");
        }else{
            return new Result(false,"删除失败");
        }

    }

    @RequestMapping("/daochu")
    public void daochubiao(HttpServletResponse response) throws  Exception{
        Poi p=new Poi();
        List list=brandService.findAll();
        p.setList(list);
        p.setTitle("品牌表");
        String [] h={"ID","品牌","首字母"};
        p.setHeaders(h);
        Object[][] o={
                {"id",POIConstant.AUTO},
                {"name", POIConstant.AUTO},
                {"firstChar",POIConstant.AUTO}
        };
        p.setFields(o);
        Poi.getResponse(response);
        p.write(response);


    }
    //实现select2方法
    @RequestMapping("/findBrandList")
    public List<Map> findBrandAll(){

        return brandService.findBrandList();
    }
}
